#include "StdAfx.h"
#include "MenuSystem.h"
#include "../../GameCore/CGame.h"

Button* Button::_bt_actived = NULL;
Button::Button()
{
	_anim = new CAnimObject();
	_posX = 0;
	_posY = 0;
	_anchor = A_NORMAL;
	_extWidth = 0;
	_extHeight = 0;
	_state = BUTTON_STATE_NORMAL;
}

void Button::Init(int state,float x, float y,int anchor,bool sound)
{
	_posX = x;
	_posY = y;
	_anchor = anchor;
	_state = state;
	_sound = sound;
}
void Button::SetAnim(CSprite *sprite,int lockID, int normalID, int selectID, int activeID)
{
	_anim->SetAnimSprite(sprite);

	_lockID = lockID;
	_normalID = normalID;
	_selectID = selectID;
	_activeID = activeID;
	SetState(_state);
}
void Button::SetAnim(CSprite *sprite,int lockID)
{
	SetAnim(sprite,lockID,lockID+1,lockID+2,lockID+3);
}
void Button::SetAnim(int spriteID,int lockID, int normalID, int selectID, int activeID)
{

	_anim->SetAnimSprite(CGame::_this->GetSprite(spriteID));

	_lockID = lockID;
	_normalID = normalID;
	_selectID = selectID;
	_activeID = activeID;
	SetState(_state);
}
void Button::SetAnim(int spriteID,int lockID)
{
	//printf("\nspriteID===%d,locID===%d",spriteID,lockID);
	SetAnim(spriteID,lockID,lockID+1,lockID+2,lockID+3);
}
void Button::SetExt(int width, int height)
{
	_extWidth = width;
	_extHeight = height;
}
float Button::GetWidth()
{
	if(!_anim->IsCurrentAnimStop())
	{
		//return _anim->GetCurrentAnimWidth();
		CSprite* _target_sprite =_anim->_anim_sprite;
		INT32 frame_index = _anim-> _current_aframes;
		INT32 frame_id = _target_sprite->_aframe_id[frame_index];
		return GetFrameWidth(_target_sprite,frame_id);
	}
	return 0;
}
float Button::GetHeight()
{
	if(!_anim->IsCurrentAnimStop())
	{
		CSprite* _target_sprite =_anim->_anim_sprite;
		INT32 frame_index = _anim-> _current_aframes;
		INT32 frame_id = _target_sprite->_aframe_id[frame_index];
		return GetFrameHeight(_target_sprite,frame_id);
	}
	return 0;
}
void Button::SetPos(float x, float y)
{
	_posX = x;
	_posY = y;
}
void Button::SetState(int state)
{
	_state = state;
	switch(state)
	{
	case BUTTON_STATE_LOCK:
		_anim->SetAnim(_lockID,true);
		break;
	case BUTTON_STATE_NORMAL:
		_anim->SetAnim(_normalID,true);
		break;
	case BUTTON_STATE_SELECT:
		if(_bt_actived != this)
		{
			if(CGame::_this->_soundEnable && _sound)
			{
				//CGame::_this->PlaySFX(SFX_BUTTON);
				//printf("\n Play sound");
			}
		}
		_bt_actived = this;
		_anim->SetAnim(_selectID,true);
		break;
	case BUTTON_STATE_ACTIVE:
		_anim->SetAnim(_activeID,true);
		_bt_actived = NULL;
		break;
	}
}
bool Button::NeedAction()
{
	if(_state == BUTTON_STATE_ACTIVE/* && _anim->IsCurrentAnimStop()*/)
	{
		//_anim->SetAnim(_activeID,false);
		return true;
	}
	return false;
}
void Button::Render(GLESLib* g)
{
	if(!_anim->IsCurrentAnimStop())
	{
		
		_anim->SetPos(_posX, _posY);
		switch(_state)
		{
			//printf("\nRUN");
			case BUTTON_STATE_LOCK:
				//printf("\nRender");
				//_anim->DrawAnimObject(g,true);
				break;
			case BUTTON_STATE_NORMAL:
				_anim->DrawAnimObject(g,true);
				break;
			case BUTTON_STATE_SELECT:
				_anim->DrawAnimObject(g,true);
				break;
			case BUTTON_STATE_ACTIVE:
				_anim->DrawAnimObject(g,false);
				break;
		}
	}
}
void Button::RenderY(GLESLib* g)
{
	//if(!_anim->IsCurrentAnimStop())
	//{
	//	
	//	_anim->SetPos(0,SCALEIPAD_Y);
	//	switch(_state)
	//	{
	//		//printf("\nRUN");
	//		case BUTTON_STATE_LOCK:
	//			//printf("\nRender");
	//			//_anim->DrawAnimObject(g,true);
	//			break;
	//		case BUTTON_STATE_NORMAL:
	//			_anim->DrawAnimObject(g,true);
	//			break;
	//		case BUTTON_STATE_SELECT:
	//			_anim->DrawAnimObject(g,true);
	//			break;
	//		case BUTTON_STATE_ACTIVE:
	//			_anim->DrawAnimObject(g,false);
	//			break;
	//	}
	//}
}
void Button::RenderXY(GLESLib* g)
{
	//if(!_anim->IsCurrentAnimStop())
	//{
	//	
	//	_anim->SetPos(SCALEIPAD_X,SCALEIPAD_Y);
	//	switch(_state)
	//	{
	//		//printf("\nRUN");
	//		case BUTTON_STATE_LOCK:
	//			//printf("\nRender");
	//			//_anim->DrawAnimObject(g,true);
	//			break;
	//		case BUTTON_STATE_NORMAL:
	//			_anim->DrawAnimObject(g,true);
	//			break;
	//		case BUTTON_STATE_SELECT:
	//			_anim->DrawAnimObject(g,true);
	//			break;
	//		case BUTTON_STATE_ACTIVE:
	//			_anim->DrawAnimObject(g,false);
	//			break;
	//	}
	//}
}
void Button::RenderX(GLESLib* g)
{
	//if(!_anim->IsCurrentAnimStop())
	//{
	//	
	//	_anim->SetPos(SCALEIPAD_X,0);
	//	switch(_state)
	//	{
	//		//printf("\nRUN");
	//		case BUTTON_STATE_LOCK:
	//			//printf("\nRender");
	//			//_anim->DrawAnimObject(g,true);
	//			break;
	//		case BUTTON_STATE_NORMAL:
	//			_anim->DrawAnimObject(g,true);
	//			break;
	//		case BUTTON_STATE_SELECT:
	//			_anim->DrawAnimObject(g,true);
	//			break;
	//		case BUTTON_STATE_ACTIVE:
	//			_anim->DrawAnimObject(g,false);
	//			break;
	//	}
	//}
}
void Button::RenderXY_4(GLESLib* g)
{
	//if(!_anim->IsCurrentAnimStop())
	//{
	//	
	//	_anim->SetPos(SCALEIPAD_X/1.2,SCALEIPAD_Y/6);
	//	switch(_state)
	//	{
	//		//printf("\nRUN");
	//		case BUTTON_STATE_LOCK:
	//			//printf("\nRender");
	//			//_anim->DrawAnimObject(g,true);
	//			break;
	//		case BUTTON_STATE_NORMAL:
	//			_anim->DrawAnimObject(g,true);
	//			break;
	//		case BUTTON_STATE_SELECT:
	//			_anim->DrawAnimObject(g,true);
	//			break;
	//		case BUTTON_STATE_ACTIVE:
	//			_anim->DrawAnimObject(g,false);
	//			break;
	//	}
	//}
}
void Button::RenderXY_2(GLESLib* g)
{
	//if(!_anim->IsCurrentAnimStop())
	//{
	//	
	//	_anim->SetPos(SCALEIPAD_X/2,SCALEIPAD_Y/1.5);
	//	switch(_state)
	//	{
	//		//printf("\nRUN");
	//		case BUTTON_STATE_LOCK:
	//			//printf("\nRender");
	//			//_anim->DrawAnimObject(g,true);
	//			break;
	//		case BUTTON_STATE_NORMAL:
	//			_anim->DrawAnimObject(g,true);
	//			break;
	//		case BUTTON_STATE_SELECT:
	//			_anim->DrawAnimObject(g,true);
	//			break;
	//		case BUTTON_STATE_ACTIVE:
	//			_anim->DrawAnimObject(g,false);
	//			break;
	//	}
	//}
}

void Button::Update()
{
	if(!_anim->IsCurrentAnimStop())
	{
		//printf("\n_amin_button");
		_anim->UpdateAnimObject();
	}
	else
	{
		//printf("state = %d",_state);
		switch(_state)
		{
			//printf("\nUpdate_Button");
			case BUTTON_STATE_LOCK:
				_anim->SetAnim(_lockID,true);
			break;
			case BUTTON_STATE_NORMAL:
				_anim->SetAnim(_normalID,true);
			break;
			case BUTTON_STATE_SELECT:
				_anim->SetAnim(_selectID,true);
			break;
			case BUTTON_STATE_ACTIVE:
				_anim->SetAnim(_selectID,true);
				break;
		}
	}
}
bool Button::CurrentAnimStop()
{
	return _anim->IsCurrentAnimStop();
}
int Button::TZ_GetX()
{
	if(!_anim->IsCurrentAnimStop())
	{
		switch(_anchor)
		{
		case A_NORMAL:
			return (_posX - _extWidth/2);
		case A_HVCENTER:
			{
				CSprite* _target_sprite =_anim->_anim_sprite;
				INT32 frame_index = _anim-> _current_aframes;
				INT32 frame_id = _target_sprite->_aframe_id[frame_index];
				//printf("\nframe_id = %d",frame_id);
				return (_posX - ((_extWidth)/2) + GetFrameX(_target_sprite,frame_id));
			}
		}
	}
	return 0;
}
int Button::TZ_GetY()
{
	if(!_anim->IsCurrentAnimStop())
	{
		switch(_anchor)
		{
		case A_NORMAL:
			return (_posY - _extHeight/2);
		case A_HVCENTER:
			{
				CSprite* _target_sprite =_anim->_anim_sprite;
				INT32 frame_index = _anim-> _current_aframes;
				INT32 frame_id = _target_sprite->_aframe_id[frame_index];
				return (_posY - ((_extHeight)/2) + GetFrameY(_target_sprite,frame_id));
			}
		}
	}
	return 0;
}
int Button::TZ_GetWidth()
{
	if(!_anim->IsCurrentAnimStop())
	{	
		return (_extWidth+GetWidth());	
	}
	return 0;
}
int Button::TZ_GetHeight()
{
	if(!_anim->IsCurrentAnimStop())
	{	
		return (_extHeight+GetHeight());	
	}
	return 0;
}

void Button::ResetPlaySound()
{
	_bt_actived = NULL;
}

int Button::GetFrameX(CSprite* spr, int frameID)
{
	int min = spr->GetFModuleX(frameID,0);
	//printf("\t spr->GetFModuleNum(%d) = %d",frameID,spr->GetFModuleNum(frameID));
	for(int i = 1; i <  spr->GetFModuleNum(frameID); i++)
	{
		if(min > spr->GetFModuleX(frameID,i))
		{
			min = spr->GetFModuleX(frameID,i);
		}
	}
	return min;
}
int Button::GetFrameY(CSprite* spr, int frameID)
{
	int min =  spr->GetFModuleY(frameID,0);
	for(int i = 0; i <  spr->GetFModuleNum(frameID); i++)
	{
		if(min > spr->GetFModuleY(frameID,i))
		{
			min = spr->GetFModuleY(frameID,i);
		}
	}
	return min;
}

int Button::GetFrameWidth(CSprite* sprite, int frame_id)
{
	int fmodule_num = sprite->GetFModuleNum(frame_id);
	float top =	sprite->GetFModuleY(frame_id,0);
	float left = sprite->GetFModuleX(frame_id,0);
	float bottom =sprite->GetFModuleY(frame_id,0)+sprite->GetFModuleHeight(frame_id,0);
	float right= sprite->GetFModuleX(frame_id,0)+sprite->GetFModuleWidth(frame_id,0);
	for (INT32 j = 0; j < fmodule_num; j++)
	{
		INT32 mid =sprite->_fmodule_id[j];
		float moduleW=sprite->GetFModuleWidth(frame_id,j);
		float moduleH= sprite->GetFModuleHeight(frame_id,j);
		float mx = sprite->GetFModuleX(frame_id,j);
		float my = sprite->GetFModuleY(frame_id,j);
		if(mx<left)left=mx;
		if(my<top)top=my;
		if((mx+moduleW)>right)right = mx+moduleW;
		if((my+moduleH)>bottom)bottom = my+moduleH;
	}
	return (right-left);
}
int Button::GetFrameHeight(CSprite* sprite, int frame_id)
{
	int fmodule_num = sprite->GetFModuleNum(frame_id);
	float top =	sprite->GetFModuleY(frame_id,0);
	float left = sprite->GetFModuleX(frame_id,0);
	float bottom =sprite->GetFModuleY(frame_id,0)+sprite->GetFModuleHeight(frame_id,0);
	float right= sprite->GetFModuleX(frame_id,0)+sprite->GetFModuleWidth(frame_id,0);
	for (INT32 j = 0; j < fmodule_num; j++)
	{
		INT32 mid =sprite->_fmodule_id[j];
		float moduleW=sprite->GetFModuleWidth(frame_id,j);
		float moduleH= sprite->GetFModuleHeight(frame_id,j);
		float mx = sprite->GetFModuleX(frame_id,j);
		float my = sprite->GetFModuleY(frame_id,j);
		if(mx<left)left=mx;
		if(my<top)top=my;
		if((mx+moduleW)>right)right = mx+moduleW;
		if((my+moduleH)>bottom)bottom = my+moduleH;
	}
	return (bottom-top);
}
int  Button::GetCurrentAnimMarkerX(CAnimObject *anim,int module)
{
	anim->CalculateCurrentAFrame();
	CSprite* _target_sprite = anim->_anim_sprite;
	INT32 frame_index = anim-> _current_aframes;
	INT32 frame_id = _target_sprite->_aframe_id[frame_index];
	return _target_sprite->GetFModuleX(frame_id,module) + _target_sprite->GetFModuleWidth(frame_id,module)/2;
}
int  Button::GetCurrentAnimMarkerY(CAnimObject *anim,int module)
{
	anim->CalculateCurrentAFrame();
	CSprite* _target_sprite = anim->_anim_sprite;
	INT32 frame_index = anim-> _current_aframes;
	INT32 frame_id = _target_sprite->_aframe_id[frame_index];
	return _target_sprite->GetFModuleY(frame_id,module) + _target_sprite->GetFModuleHeight(frame_id,module)/2;
}
 int Button::GetModuleMarkerX(CSprite* _target_sprite,int frame_id,int module)
{
	return _target_sprite->GetFModuleX(frame_id,module) + _target_sprite->GetFModuleWidth(frame_id,module)/2;
}
 int Button::GetModuleMarkerY(CSprite* _target_sprite,int frame_id,int module)
{
	return _target_sprite->GetFModuleY(frame_id,module) + _target_sprite->GetFModuleHeight(frame_id,module)/2;
}
/* int Button::GetCurrentAnimFModuleAngle(CAnimObject *anim,int module)
{
	anim->CalculateCurrentAFrame();
	CSprite* _target_sprite = anim->_anim_sprite;
	INT32 frame_index = anim-> _current_aframes;
	INT32 frame_id = _target_sprite->_aframe_id[frame_index];
	return _target_sprite->GetFModuleAngle(frame_id,module);
}
 int Button::GetModuleOpacity(CSprite *sprite,int frame_id, int moduleOfFrame)
 {

	return sprite->GetFModuleOpacity(frame_id,moduleOfFrame);
 }
 int Button::GetModuleAngle(CSprite *sprite,int frame_id, int moduleOfFrame)
 {
	 return sprite->GetFModuleAngle(frame_id,moduleOfFrame);
 }
 int Button::GetModuleTransform(CSprite *sprite,int frame_id, int moduleOfFrame)
 {
	 return sprite->GetTranslate(frame_id,moduleOfFrame);
 }*/
int Button::GetCurrentAnimFModuleX(CAnimObject *anim,int module)
{
	anim->CalculateCurrentAFrame();
	CSprite* _target_sprite = anim->_anim_sprite;
	INT32 frame_index = anim-> _current_aframes;
	INT32 frame_id = _target_sprite->_aframe_id[frame_index];
	return _target_sprite->GetFModuleX(frame_id,module);
}
int Button::GetCurrentAnimFModuleY(CAnimObject *anim,int module)
{
	anim->CalculateCurrentAFrame();
	CSprite* _target_sprite = anim->_anim_sprite;
	INT32 frame_index = anim-> _current_aframes;
	INT32 frame_id = _target_sprite->_aframe_id[frame_index];
	return _target_sprite->GetFModuleY(frame_id,module);
}

int Button::GetCurrentAnimFModuleWidth(CAnimObject *anim,int module)
{
	anim->CalculateCurrentAFrame();
	CSprite* _target_sprite = anim->_anim_sprite;
	INT32 frame_index = anim-> _current_aframes;
	INT32 frame_id = _target_sprite->_aframe_id[frame_index];
	return _target_sprite->GetFModuleWidth(frame_id,module);
}
int Button::GetCurrentAnimFModuleHeight(CAnimObject *anim,int module)
{
	anim->CalculateCurrentAFrame();
	CSprite* _target_sprite = anim->_anim_sprite;
	INT32 frame_index = anim-> _current_aframes;
	INT32 frame_id = _target_sprite->_aframe_id[frame_index];
	return _target_sprite->GetFModuleHeight(frame_id,module);
}

int Button::GetCurrentAnimFrameX(CAnimObject *anim,int frameID)
{
	anim->CalculateCurrentAFrame();
	CSprite* _target_sprite = anim->_anim_sprite;
	INT32 frame_index = anim-> _current_aframes;
	INT32 frame_id = _target_sprite->_aframe_id[frame_index];
	return GetFrameX(_target_sprite,frame_id);
}
int Button::GetCurrentAnimFrameY(CAnimObject *anim,int frameID)
{
	anim->CalculateCurrentAFrame();
	CSprite* _target_sprite = anim->_anim_sprite;
	INT32 frame_index = anim-> _current_aframes;
	INT32 frame_id = _target_sprite->_aframe_id[frame_index];
	return GetFrameY(_target_sprite,frame_id);
}
int Button::GetCurrentAnimFrameWidth(CAnimObject *anim,int frameID)
{
	anim->CalculateCurrentAFrame();
	CSprite* _target_sprite = anim->_anim_sprite;
	INT32 frame_index = anim-> _current_aframes;
	INT32 frame_id = _target_sprite->_aframe_id[frame_index];
	return GetFrameWidth(_target_sprite,frame_id);
}
int Button::GetCurrentAnimFrameHeight(CAnimObject *anim,int frameID)
{
	anim->CalculateCurrentAFrame();
	CSprite* _target_sprite = anim->_anim_sprite;
	INT32 frame_index = anim-> _current_aframes;
	INT32 frame_id = _target_sprite->_aframe_id[frame_index];
	return GetFrameHeight(_target_sprite,frame_id);
}

int Button::GetModuleID(CSprite *sprite,int frame_id, int moduleOfFrame)
{
	INT32 fmodule_min = 0;
	INT32 fmodule_max = 0;
	
	if (frame_id < (sprite->_num_frames - 1))
	{
		fmodule_min = sprite->_frame_offset[frame_id];
		fmodule_max = sprite->_frame_offset[frame_id + 1] - 1;
	}
	else if (frame_id == (sprite->_num_frames - 1))
	{
		fmodule_min = sprite->_frame_offset[frame_id];
		fmodule_max = sprite->_num_fmodules - 1;
	}
	return sprite->_fmodule_id[fmodule_min+moduleOfFrame];
}



/////////////////////////////Math Utils ///////////////////////////////////////////

void CGame::InitSinTable()
{
	for(int i = 0; i < MAX_TABLE; i++)
	{
		_sin_table[i] = CMath::SIN(0.1*i);
	}
}
void CGame::InitCosTable()
{
	for(int i = 0; i < MAX_TABLE; i++)
	{
		_cos_table[i] = CMath::COS(0.1*i);
	}
}

void CGame::InitMathTable()
{
	InitSinTable();
	InitCosTable();
}

float CGame::Math_Cos(float angle)
{
	while(angle < 0)
	{
		angle += 360;
	}
	while(angle >= 360)
	{
		angle -= 360;
	}
	return _cos_table[(int)(angle/0.1)];
}

float CGame::Math_Sin(float angle)
{
	while(angle < 0)
	{
		angle += 360;
	}
	while(angle >= 360)
	{
		angle -= 360;
	}
	return _sin_table[(int)(angle/0.1)];
}

float CGame::Math_AngleAbs(float angle)
{
	float offset =(angle - (int)angle);
	angle = (int)(angle)%360 + offset;
	if(angle < 0 || angle >= 360)
	{
		while(angle < 0)
		{
			angle += 360;
		}
		while(angle >= 360)
		{
			angle -= 360;
		}
	}
	return angle;
}


